package com.cpr.core.rabbitmq.client.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @author Chen Panrui
 * created at 2022/3/15 22:01
 *
 * 话题 bk = 点分单词
 * # = 匹配多个
 * * = 匹配一个单词
 *
 * shanghai.emp-online.info
 */
public class Producer {

    private static final String[] LOG_LEVEL = {"info","warn","error"};
    private static final String[] LOG_AREA = {"beijing","shanghai","shenzhen"};
    private static final String[] LOG_BIZ = {"edu-online","biz-online","emp-online"};

    private static final Random RANDOM = new Random();

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("ex.topic", BuiltinExchangeType.TOPIC, true, false, null);
        String rk = "", message = "";
        String area, level, biz;

        for (int i = 0; i < 100; i++) {
            area = LOG_AREA[RANDOM.nextInt(100) % LOG_AREA.length];
            level = LOG_LEVEL[RANDOM.nextInt(100) % LOG_LEVEL.length];
            biz = LOG_BIZ[RANDOM.nextInt(100) % LOG_BIZ.length];

            rk = area + "." + biz + "." + level;
            message = i + "打印日志LOG:[" + rk + "]";

            channel.basicPublish("ex.topic", rk, null, message.getBytes(StandardCharsets.UTF_8));
        }

        channel.close();
        connection.close();
    }
}
